package org.nemanjaristic.database;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
import org.nemanjaristic.utils.TimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HibernateSessionRequestFilter implements Filter {

	private static final Logger logger = LoggerFactory.getLogger(HibernateSessionRequestFilter.class);

	private SessionFactory sf;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		long start = System.nanoTime();
		
		try {
			logger.debug("Request received ...");
			logger.debug("Starting a database transaction.");
			sf.getCurrentSession().beginTransaction();

			// Call the next filter (continue request processing)
			chain.doFilter(request, response);

			// Commit and cleanup
			logger.debug("Committing the database transaction.");
			sf.getCurrentSession().getTransaction().commit();

		}
		catch (StaleObjectStateException staleEx) {
			// logger.error("This interceptor does not implement optimistic concurrency control !");
			// logger.error("Your application will not work until you add compensation actions !");
			// Rollback, close everything, possibly compensate for any permanent
			// changes during the conversation, and finally restart business
			// conversation. Maybe give the user of the application a chance to
			// merge some of his work with fresh data ... what you do here
			// depends on your applications design.
			
			logger.error("Problem with entity {} with id {}.", staleEx.getEntityName(), staleEx.getIdentifier());
			logger.error("Exception description: {}", staleEx.getMessage());
			
			try {
				if (sf.getCurrentSession().getTransaction().isActive()) {
					logger.debug("Trying to rollback database transaction after exception.");
					sf.getCurrentSession().getTransaction().rollback();
				}
			}
			catch (Throwable rbEx) {
				logger.error("Could not rollback transaction after exception ! {}", rbEx.getMessage());
			}
			
			throw staleEx;
		}
		catch (Throwable ex) {
			// Rollback only
			logger.error(ex.getMessage());
			//ex.printStackTrace();
			try {
				if (sf.getCurrentSession().getTransaction().isActive()) {
					logger.debug("Trying to rollback database transaction after exception.");
					sf.getCurrentSession().getTransaction().rollback();
				}
			}
			catch (Throwable rbEx) {
				logger.error("Could not rollback transaction after exception ! {}", rbEx.getMessage());
			}

			// Let others handle it ... maybe another interceptor for exceptions ?
			throw new ServletException(ex);
		}
		finally {
			long end = System.nanoTime();
			
			logger.debug("Request finished in {}", TimeUtils.nsToString(end - start));
		}
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		logger.debug("Initializing filter ...");
		logger.debug("Obtaining SessionFactory from static HibernateUtil singleton.");
		
		sf = HibernateUtil.getSessionFactory();
	}

	@Override
	public void destroy() {}
}
